/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JPanelRating.java
 *
 * Created on Dec 4, 2011, 11:58:01 AM
 */
package recitation;

import java.awt.*;
import javax.swing.*;
import target.Rating;
import target.Word;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

/**
 *
 * @author buer
 */
public class JPanelRating extends javax.swing.JPanel {

    /** Creates new form JPanelRating */
    public JPanelRating(Word oneWord) {
        this.oneWord = oneWord;
        initComponents();
        jPStars.setLayout(new FlowLayout(0,0,0));
        jPStars.addMouseListener(new TMouseListener());
        paintStars(this.oneWord.getRating());
    }
    
    public void updateContent(Word word) {
        this.oneWord = word;
        jPStars.removeAll();
        paintStars(oneWord.getRating());
    }

    /*
     * @Assert 0 < stars <= Rating.getMax()
     */
    private void paintStars(int stars) {
        // When assertion is violated, ...
        if (stars < 0 || stars > Rating.getMax()) {
            stars = Rating.getMax();
        }
        // count: stars
        for (int i = 0; i < stars; i++) {
            Image img = new ImageIcon("images/newstars.png").getImage();
            ImagePanel jp = new ImagePanel(img);
            jPStars.add(jp);
        }
        // count: Max - stars;
        for (int i = stars; i < Rating.getMax(); i++) {
            Image img = new ImageIcon("images/stars.png").getImage();
            ImagePanel jp = new ImagePanel(img);
            jPStars.add(jp);
        }
    }
    
    /**
     * 将Word的rating值设为用户给的评级。
     * @param n 
     */
    private void setCount(int n){
        this.oneWord.rating = n;
    }
    
    private int getImageSizeX() {
        Image img = new ImageIcon("images/stars.png").getImage();
        int size = img.getWidth(null);
        return size;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLRating = new javax.swing.JLabel();
        jPStars = new javax.swing.JPanel();

        setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true));
        setPreferredSize(new java.awt.Dimension(200, 150));

        jLRating.setText("Rating");

        jPStars.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPStars, javax.swing.GroupLayout.DEFAULT_SIZE, 174, Short.MAX_VALUE)
                    .addComponent(jLRating))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLRating)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPStars, javax.swing.GroupLayout.DEFAULT_SIZE, 30, Short.MAX_VALUE)
                .addGap(82, 82, 82))
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLRating;
    private javax.swing.JPanel jPStars;
    // End of variables declaration//GEN-END:variables
    private Word oneWord;
    
    class TMouseListener implements MouseListener {

        @Override
        public void mouseClicked(MouseEvent e) {
            int x = e.getX();
            int y = e.getY();
            int size = getImageSizeX();
            int count = (int)x/size + 1;
            
            jPStars.removeAll();
            paintStars(count);
            setCount(count);
            updateUI();
        }

        @Override
        public void mouseEntered(MouseEvent e) {
        }

        @Override
        public void mouseReleased(MouseEvent e) {
        }

        @Override
        public void mousePressed(MouseEvent e) {
        }

        @Override
        public void mouseExited(MouseEvent e) {
        }
    }
}
